package main

import (
	"fmt"
	"sort"
)

func solution(stones []int) int {
	if len(stones) == 1 {
		return 0
	}

	sort.Ints(stones)

	var gaps []int
	for i := 1; i < len(stones); i++ {
		gaps = append(gaps, stones[i]-stones[i-1]-1)
	}
	var gapsList []int
	if gaps[0] > gaps[len(gaps)-1] {
		gapsList = gaps[:len(gaps)-1]
	} else {
		gapsList = gaps[1:]
	}

	sum := 0

	for _, val := range gapsList {
		sum += val
	}

	return sum
}

func main() {
	fmt.Println(solution([]int{7, 4, 9}) == 2)
	fmt.Println(solution([]int{6, 5, 4, 3, 10}) == 3)
	fmt.Println(solution([]int{1, 2, 3, 4, 5}) == 0)
}
